package com.example.java2e.Controller;

import com.example.java2e.Entity.Message;
import com.example.java2e.Entity.Notice;
import com.example.java2e.Entity.Tip_off;
import com.example.java2e.Entity.User;
import com.example.java2e.Service.NoticeService;
import com.example.java2e.Service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
public class NoticeController {
    @Autowired
    private NoticeService noticeService;
    @Autowired
    private UserService userService;

    @PostMapping("/send_notice")
    public Map<String, Object> send_notice(HttpServletRequest request, @RequestBody Map<String, Object> front_map) {
        String title = (String) front_map.get("title");
        String content = (String) front_map.get("content");
        String user_receive = (String) front_map.get("id_receive");
        HttpSession session = request.getSession();
        String user_send = (String) session.getAttribute("id");
        Date date_object = new Date();
        SimpleDateFormat date_format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String date = date_format.format(date_object);
        Map<String, Object> back_map = new HashMap<>();
        try {
            if (title.equals("")) {
                back_map.put("code", "标题不能为空");
            }
            else {
                if (user_receive.equals("")) {
                    // 发送给所有学生
                    Notice notice = new Notice(user_send, title, content, date, "all");
                    noticeService.insertNewNotice(notice);
                    back_map.put("date", date);
                    back_map.put("code", '1');
                }
                else {
                    User temp_user = userService.selectUserById(user_receive);
                    if (temp_user == null) {
                        back_map.put("code", "学生不存在");
                    }
                    else {
                        // 发送给某个学生
                        Notice notice = new Notice(user_send, title, content, date, user_receive);
                        noticeService.insertNewNotice(notice);
                        back_map.put("date", date);
                        back_map.put("code", '1');
                    }
                }
            }
        }catch (Exception e){
            System.out.println(e);
            back_map.put("code", "发送失败");
        }
        return back_map;
    }

    @PostMapping("/delete_notice")
    public Map<String, Object> delete_notice(@RequestBody Map<String, Object> front_map) {
        String title = (String) front_map.get("title");
        String date = (String) front_map.get("date");
        Map<String, Object> back_map = new HashMap<>();
        try{
            noticeService.deleteNotice(title, date);
            List<Notice> noticeList = noticeService.selectAllNotice();
            int length = noticeList.size();
            System.out.println(length);
            for (int i = 0; i < length; i++) {
                String[] temp = {noticeList.get(i).getTitle(), noticeList.get(i).getDate()};
                back_map.put(noticeList.get(i).getId().toString(), temp);
            }
        }catch (Exception e) {
            System.out.println(e);
        }
        return back_map;
    }

    @PostMapping("/check_notice_manager")
    public Map<String, Object> check_notice_manager(@RequestBody Map<String, Object> front_map) {
        Map<String, Object> back_map = new HashMap<>();
        try {
            List<Notice> noticeList2 = noticeService.selectAllNotice();
            int length = noticeList2.size();
            System.out.println(length);
            for (int i = 0; i < length; i++) {
                String[] temp = {noticeList2.get(i).getTitle(), noticeList2.get(i).getDate()};
                back_map.put(noticeList2.get(i).getId().toString(), temp);
            }
        }catch (Exception e) {
            System.out.println(e);
        }
        return back_map;
    }

    @PostMapping("/check_notice_detail_manager")
    public Map<String, Object> check_notice_detail_manager(@RequestBody Map<String, Object> front_map) {
        String title = (String) front_map.get("title");
        String date = (String) front_map.get("date");
        Map<String, Object> back_map = new HashMap<>();
        try {
            Notice notice = noticeService.selectNoticeByTitleAndDate(title, date);
            back_map.put("content", notice.getContent());
            if (notice.getId_receive().equals("all")) {
                back_map.put("id_receive", "全部用户");
            }
            else {
                back_map.put("id_receive", notice.getId_receive());
            }
        }catch (Exception e) {
            System.out.println(e);
        }
        return back_map;
    }

    @PostMapping("/get_notice")
    public Map<String, Object> get_notice(HttpServletRequest request, @RequestBody Map<String, Object> front_map) {
        Map<String, Object> back_map = new HashMap<>();
        HttpSession session = request.getSession();
        String user_id = (String) session.getAttribute("id");
        try {
            List<Notice> noticeList3 = noticeService.selectNoticeForUser(user_id);
            int length = noticeList3.size();
            System.out.println(length);
            for (int i = 0; i < length; i++) {
                String[] temp = {noticeList3.get(i).getTitle(), noticeList3.get(i).getDate()};
                back_map.put(noticeList3.get(i).getId().toString(), temp);
            }
        }catch (Exception e) {
            System.out.println(e);
        }
        return back_map;
    }

    @PostMapping("/get_notice_detail")
    public Map<String, Object> get_notice_detail(@RequestBody Map<String, Object> front_map) {
        String title = (String) front_map.get("title");
        Map<String, Object> back_map = new HashMap<>();
        try {
            Notice notice = noticeService.selectNoticeByTitle(title);
            if (notice != null) {
                back_map.put("content", notice.getContent());
            }
        }catch (Exception e) {
            System.out.println(e);
        }
        return back_map;
    }

    @PostMapping("/check_message_manager")
    public Map<String, Object> check_message_manager(@RequestBody Map<String, Object> front_map) {
        Map<String, Object> back_map = new HashMap<>();
        try {
            List<Message> messageList = noticeService.selectAllMessage();
            int length = messageList.size();
            for (int i = 0; i < length; i++) {
                User temp_user = userService.selectUserById(messageList.get(i).getId_send());
                if (temp_user != null) {
                    Message temp_message = noticeService.selectMessageByIdReply(messageList.get(i).getId());
                    if (temp_message != null) {
                        String[] temp = {messageList.get(i).getId_send(), messageList.get(i).getTitle(),
                                messageList.get(i).getContent(), messageList.get(i).getDate(), temp_message.getContent(),
                                temp_message.getDate()};
                        back_map.put(messageList.get(i).getId().toString(), temp);
                    }
                    else {
                        String[] temp = {messageList.get(i).getId_send(), messageList.get(i).getTitle(),
                                messageList.get(i).getContent(), messageList.get(i).getDate(), "", ""};
                        back_map.put(messageList.get(i).getId().toString(), temp);
                    }
                }
            }
        }catch (Exception e) {
            System.out.println(e);
        }
        return back_map;
    }

    @PostMapping("/check_user_feedback")
    public Map<String, Object> check_user_feedback(HttpServletRequest request, @RequestBody Map<String, Object> front_map) {
        HttpSession session = request.getSession();
        String user_id = (String) session.getAttribute("id");
        Map<String, Object> back_map = new HashMap<>();
        try {
            List<Message> messageList = noticeService.selectMessageByIdSend(user_id);
            int length = messageList.size();
            for (int i = 0; i < length; i++) {
                Message temp_message = noticeService.selectMessageByIdReply(messageList.get(i).getId());
                if (temp_message != null) {
                    String[] temp = {messageList.get(i).getTitle(),
                            messageList.get(i).getContent(), messageList.get(i).getDate(), temp_message.getContent(),
                            temp_message.getDate()};
                    back_map.put(messageList.get(i).getId().toString(), temp);
                }
                else {
                    String[] temp = {messageList.get(i).getTitle(),
                            messageList.get(i).getContent(), messageList.get(i).getDate(), "", ""};
                    back_map.put(messageList.get(i).getId().toString(), temp);
                }
            }
        }catch (Exception e) {
            System.out.println(e);
        }
        return back_map;
    }

    @PostMapping("/manager_reply_message")
    public Map<String, Object> manager_reply_message(HttpServletRequest request, @RequestBody Map<String, Object> front_map) {
        String title = (String) front_map.get("title");
        String content = (String) front_map.get("content");
        String date = (String) front_map.get("date");
        String id_send = (String) front_map.get("id_send");
        Map<String, Object> back_map = new HashMap<>();
        try {
            Message message = noticeService.selectMessageByTitleDateAndIdSend(title, date, id_send);
            HttpSession session = request.getSession();
            String user_id = (String) session.getAttribute("id");
            Date date_object = new Date();
            SimpleDateFormat date_format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String now_date = date_format.format(date_object);
            Message new_message = new Message(user_id, "", content, message.getId(), 1, now_date);
            noticeService.insertNewMessage(new_message);
            Message new_message2 = noticeService.selectMessageByTitleDateAndIdSend("", now_date, user_id);
            noticeService.modifyIdReply(message.getId(), new_message2.getId());

            List<Message> messageList = noticeService.selectAllMessage();
            int length = messageList.size();
            for (int i = 0; i < length; i++) {
                if (messageList.get(i).getRead_or_not() == 0) {
                    if (messageList.get(i).getId_reply() != -1) {
                        Message message_reply = noticeService.selectMessageById(messageList.get(i).getId_reply());
                        String[] temp = {messageList.get(i).getId_send(), messageList.get(i).getTitle(),
                                messageList.get(i).getContent(), messageList.get(i).getDate(),
                                message_reply.getContent(), message_reply.getDate()};
                        back_map.put(messageList.get(i).getId().toString(), temp);
                    }
                    else {
                        String[] temp = {messageList.get(i).getId_send(), messageList.get(i).getTitle(),
                                messageList.get(i).getContent(), messageList.get(i).getDate(), "", ""};
                        back_map.put(messageList.get(i).getId().toString(), temp);
                    }
                }
            }
            back_map.put("code", "1");
        }catch (Exception e) {
            System.out.println(e);
        }
        return back_map;
    }

    @PostMapping("/user_send_message")
    public Map<String, Object> user_send_message(HttpServletRequest request, @RequestBody Map<String, Object> front_map) {
        HttpSession session = request.getSession();
        String id_send = (String) session.getAttribute("id");
        String title = (String) front_map.get("title");
        String content = (String) front_map.get("content");
        Date date_object = new Date();
        SimpleDateFormat date_format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String now_date = date_format.format(date_object);
        Map<String, Object> back_map = new HashMap<>();
        try {
            if (title.equals("")) {
                back_map.put("code", "标题不能为空");
            }
            else {
                Message new_message = new Message(id_send, title, content, (long) -1, 0, now_date);
                noticeService.insertNewMessage(new_message);

                List<Message> messageList = noticeService.selectAllMessage();
                int length = messageList.size();
                for (int i = 0; i < length; i++) {
                    if (messageList.get(i).getRead_or_not() == 0) {
                        if (messageList.get(i).getId_reply() != -1) {
                            Message message_reply = noticeService.selectMessageById(messageList.get(i).getId_reply());
                            String[] temp = {messageList.get(i).getId_send(), messageList.get(i).getTitle(),
                                    messageList.get(i).getContent(), messageList.get(i).getDate(),
                                    message_reply.getContent(), message_reply.getDate()};
                            back_map.put(messageList.get(i).getId().toString(), temp);
                        }
                        else {
                            String[] temp = {messageList.get(i).getId_send(), messageList.get(i).getTitle(),
                                    messageList.get(i).getContent(), messageList.get(i).getDate(), "", ""};
                            back_map.put(messageList.get(i).getId().toString(), temp);
                        }
                    }
                }
                back_map.put("code", "1");
            }
        }catch (Exception e){
            System.out.println(e);
        }
        return back_map;
    }

    @PostMapping("/send_tip_off")
    public Map<String, Object> send_tip_off(HttpServletRequest request, @RequestBody Map<String, Object> front_map) {
        HttpSession session = request.getSession();
        String id_send = (String) session.getAttribute("id");
        String id_driver = (String) front_map.get("id_driver");
        String id_vehicle = (String) front_map.get("id_vehicle");
        Long id_route = Long.parseLong((String) front_map.get("id_route"));
        String reason = (String) front_map.get("reason");
        Date date_object = new Date();
        SimpleDateFormat date_format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String now_date = date_format.format(date_object);
        Map<String, Object> back_map = new HashMap<>();
        try {
            Tip_off tip_off = new Tip_off(id_send, id_driver, id_vehicle, id_route, reason, now_date, 0);
            noticeService.insertNewTipOff(tip_off);
            back_map.put("code", "1");
        }catch (Exception e) {
            back_map.put("code", "0");
            System.out.println(e);
        }
        return back_map;
    }

    @PostMapping("/delete_tip_off")
    public Map<String, Object> delete_tip_off(@RequestBody Map<String, Object> front_map) {
        String id_driver = (String) front_map.get("id_driver");
        String id_stu = (String) front_map.get("id_stu");
        String date = (String) front_map.get("date");
        Map<String, Object> back_map = new HashMap<>();
        try {
            noticeService.deleteTipOffByDriverStuAndDate(id_driver, id_stu, date);
            List<Tip_off> tipOffList = noticeService.selectAllTipOff();
            int length = tipOffList.size();
            for (int i = 0; i < length; i++) {
                String con = "";
                if (tipOffList.get(i).getTip_off_condition() == 1) {
                    con = "已处理";
                }
                else {
                    con = "未处理";
                }
                String[] temp = {tipOffList.get(i).getId_driver(), tipOffList.get(i).getId_stu(), tipOffList.get(i).getDate(), con};
                back_map.put(tipOffList.get(i).getId().toString(), temp);
            }
        }catch (Exception e) {
            System.out.println(e);
        }
        return back_map;
    }

    @PostMapping("/change_tip_off_condition")
    public Map<String, Object> change_tip_off_condition(@RequestBody Map<String, Object> front_map) {
        String id_driver = (String) front_map.get("id_driver");
        String id_stu = (String) front_map.get("id_stu");
        String date = (String) front_map.get("date");
        int new_condition = Integer.parseInt((String) front_map.get("new_condition"));
        Map<String, Object> back_map = new HashMap<>();
        try {
            noticeService.modifyTipOffCondition(id_driver, id_stu, date, new_condition);
            List<Tip_off> tipOffList = noticeService.selectAllTipOff();
            int length = tipOffList.size();
            for (int i = 0; i < length; i++) {
                String con = "";
                if (tipOffList.get(i).getTip_off_condition() == 1) {
                    con = "已处理";
                }
                else {
                    con = "未处理";
                }
                String[] temp = {tipOffList.get(i).getId_driver(), tipOffList.get(i).getId_stu(), tipOffList.get(i).getDate(), con};
                back_map.put(tipOffList.get(i).getId().toString(), temp);
            }
        }catch (Exception e) {
            System.out.println(e);
        }
        return back_map;
    }

    @PostMapping("/check_tip_off_detail")
    public Map<String, Object> check_tip_off_detail(@RequestBody Map<String, Object> front_map) {
        String id_driver = (String) front_map.get("id_driver");
        String id_stu = (String) front_map.get("id_stu");
        String date = (String) front_map.get("date");
        Map<String, Object> back_map = new HashMap<>();
        try {
            Tip_off tip_off = noticeService.selectTipOffByDriverStuAndDate(id_driver, id_stu, date);
            back_map.put("id_vehicle", tip_off.getId_vehicle());
            back_map.put("id_route", tip_off.getId_route());
            back_map.put("reason", tip_off.getReason());
            if (tip_off.getTip_off_condition() == 1) {
                back_map.put("condition", "已处理");
            }
            else {
                back_map.put("condition", "未处理");
            }
        }catch (Exception e) {
            System.out.println(e);
        }
        return back_map;
    }

    @PostMapping("/check_tip_off")
    public Map<String, Object> check_tip_off(@RequestBody Map<String, Object> front_map) {
        Map<String, Object> back_map = new HashMap<>();
        try {
            List<Tip_off> tipOffList = noticeService.selectAllTipOff();
            int length = tipOffList.size();
            for (int i = 0; i < length; i++) {
                String con = "";
                if (tipOffList.get(i).getTip_off_condition() == 1) {
                    con = "已处理";
                }
                else {
                    con = "未处理";
                }
                String[] temp = {tipOffList.get(i).getId_driver(), tipOffList.get(i).getId_stu(), tipOffList.get(i).getDate(), con};
                back_map.put(tipOffList.get(i).getId().toString(), temp);
            }
        }catch (Exception e) {
            System.out.println(e);
        }
        return back_map;
    }
}
